next up previous contents index
Next: The For..to/downto..do statement Up: Statements controlling program flow. Previous: Assignments

The Case statement

Free Pascal supports the case statement. Its prototype is

Case Pivot of
  Label1 : Statement1;
  Label2 : Statement2;
  ...
  Labeln : Statementn;
[Else
  AlternativeStatement]
end;
label1 until Labeln must be known at compile-time, and can be of the following types : enumeration types, Ordinal types (except boolean), and chars. Pivot must also be one of these types.

The statements Statement1 etc., can be compound statements (i.e. a begin..End block).

Remark: Contrary to Turbo Pascal, duplicate case labels are not allowed in Free Pascal, so the following code will generate an error when compiling:

Var i : integer;
...

Case i of
 3 : DoSomething;
 1..5 : DoSomethingElse;
end;
The compiler will generate a Duplicate case label error when compiling this, because the 3 also appears (implicitly) in the range 1..5

Remark: In versions earlier than 0.9.7, there was an incompatibility here with Turbo Pascal. Where in Turbo Pascal you could do the following:

case Pivot of
  ...
Else
  begin
  Statement1
  Statement2
  end;
You needed to do the following in Free Pascal :
case Pivot of
  ...
Else
  begin
  Statement1
  Statement2
  end;
end;
So there's an extra end keyword at the end. But from version 0.9.7 this has been fixed.

Michael Van Canneyt
Tue Mar 31 16:48:49 CEST 1998